package com.gduf.lsm.service;


import cn.hutool.core.util.ObjectUtil;
import com.gduf.lsm.common.constant.ApplyServeStatusConstant;
import com.gduf.lsm.common.enums.ResultCodeEnum;
import com.gduf.lsm.entity.ApplyHousework;
import com.gduf.lsm.entity.ApplyMaintain;
import com.gduf.lsm.exception.CustomException;
import com.gduf.lsm.mapper.ApplyMaintainMapper;
import com.gduf.lsm.mapper.ServeMapper;
import com.gduf.lsm.pojo.dto.ApplyServeStatusDTO;
import com.gduf.lsm.pojo.dto.EvaluationDTO;
import com.gduf.lsm.pojo.vo.ApplyHouseworkShowVO;
import com.gduf.lsm.pojo.vo.ApplyMaintainShowVO;
import com.gduf.lsm.pojo.vo.EvaluationVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 维修服务预约业务处理
 **/
@Service
public class ApplyMaintainService {

    @Autowired
    private ApplyMaintainMapper applyMaintainMapper;
    @Autowired
    private ServeMapper serveMapper;

    /**
     * 新增
     * @param applyMaintain
     */
    public void add(ApplyMaintain applyMaintain) {
        //维修申请状态为已申请，用户ID和服务ID不可为空
        boolean flag=(!applyMaintain.getStatus().equals(ApplyServeStatusConstant.APPLY_SERVE_STATUS_APPLIED))|| ObjectUtil.isNull(applyMaintain.getUserId())||ObjectUtil.isNull(applyMaintain.getSerId());
        if(flag){
            throw new CustomException(ResultCodeEnum.APPLY_MAINTAIN_NULL_ERROR);
        }
        applyMaintain.setApplyTime(applyMaintain.getApDate().substring(0,10)+" "+applyMaintain.getApTime());
        applyMaintainMapper.insert(applyMaintain);
    }

    /**
     * 删除
     * @param id
     */
    public void deleteById(Integer id) {
        applyMaintainMapper.deleteById(id);
    }

    /**
     * 批量删除
     * @param ids
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            applyMaintainMapper.deleteById(id);
        }
    }

    /**
     * 修改
     * @param applyMaintain
     */
    public void updateById(ApplyMaintain applyMaintain) {
        applyMaintainMapper.updateById(applyMaintain);
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    public ApplyMaintain selectById(Integer id) {
        return applyMaintainMapper.selectById(id);
    }

    /**
     * 查询所有
     * @param applyMaintain
     * @return
     */
    public List<ApplyMaintain> selectAll(ApplyMaintain applyMaintain) {
        return applyMaintainMapper.selectAll(applyMaintain);
    }

    /**
     * 按用户ID查询参与过的服务，映射VO
     * @param userId
     * @return
     */
    public List<ApplyMaintainShowVO> selectByUserId(Integer userId) {
        ApplyMaintain applyMaintain=new ApplyMaintain();
        applyMaintain.setUserId(userId);
        List<ApplyMaintainShowVO> list=applyMaintainMapper.selectPageQuery(applyMaintain);
        return list;
    }

    /**
     * 分页查询
     * 有对应VO
     * @param applyMaintain
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<ApplyMaintainShowVO> selectPage(ApplyMaintain applyMaintain, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ApplyMaintainShowVO> list = applyMaintainMapper.selectPageQuery(applyMaintain);
        return PageInfo.of(list);
    }

    /**
     * 修改表状态，因为直接用update的话，含数据部分总会被设置为空，因此改过来
     * 已投诉，已处理
     * @param applyServeStatusDTO
     */
    public void updateStatus(ApplyServeStatusDTO applyServeStatusDTO) {
        applyMaintainMapper.updateStatus(applyServeStatusDTO);
    }

    /**
     * 修改表状态，管理员处理已预约
     * @param applyServeStatusDTO
     */
    public void updateHandle(ApplyServeStatusDTO applyServeStatusDTO){
        applyMaintainMapper.updateHandle(applyServeStatusDTO);
    }

    /**
     * 用户已评价
     * @param applyServeStatusDTO
     */
    public void updateEvaluation(ApplyServeStatusDTO applyServeStatusDTO) {
        //先修改状态与评价内容
        /*int gradeNum=Integer.parseInt(applyServeStatusDTO.getGrade());
        EvaluationDTO evaluationDTO=new EvaluationDTO();
        evaluationDTO.setId(applyServeStatusDTO.getId());
        evaluationDTO.setGrade(gradeNum);
        evaluationDTO.setEvaluation(applyServeStatusDTO.getEvaluation());*/
        applyMaintainMapper.updateEvaluation(applyServeStatusDTO);
        //再计算评价平均分数，修改对应服务表的内容
        float grade=applyMaintainMapper.calGrade();
        float roundGrade=Math.round(grade*10)/10.0f;
        serveMapper.updateGradeById(applyServeStatusDTO.getSerId(),roundGrade);
    }

    /**
     * 获取评价
     * @return
     */
    public List<EvaluationVO> getEvaluations() {
        return applyMaintainMapper.getEvaluations();
    }
}